import SystemUtil from '../../shell/system/system'
import SessionUtil, { JxbHttpClient } from '../../data/session/session';
import SessionSellingServerUtil from '../../data/session/sessionSellingServer';

Page({
    data: {
        token: '',
        ms_tablist: [],
        ms_interval:"",
    },
    onLoad: function (options) {
        let activityId = options.activity_id
        SessionUtil.getInstance().then(session=>{
            let sellingServer = SessionSellingServerUtil.getInstanceSync(),
                vCard = sellingServer.getTheVCardSync()
            wx.setNavigationBarTitle({
                title: vCard.shopName + '活动专场'
            })
            this.setData({
                token: session.token,
                activity_id: activityId,            
            });

            /**获取屏幕宽度 */
            let w_width = SystemUtil.getSystemInfo(false).windowWidth
            this.setData({
                w_width: w_width,
                toleft: w_width * 0.4,
                width: w_width * 0.6
            })

            /**获取活动信息 */
            this.getActiveInfo(activityId);

            // 页面加载进来的时候获取活动名称
            this.getAcname(activityId);
        })
    },
    
    my_start: function (e) {
        this.setData({
            startPoint: e.touches[0].clientX
        })
    },
    my_end: function (e) {
        var startPoint = this.data.startPoint;
        var endPoint = e.changedTouches[0].clientX;
        var distance = startPoint - endPoint;
        var ms_move = this.data.w_width * 0.6;
        var ms_index = this.data.js_index;
        var left_click = this.data.w_width * 0.2;
        var right_click = this.data.w_width * 0.8;
        
        if (Math.abs(distance) < 5){
            /**点击左边的 */
            if (endPoint <= left_click){
                if (ms_index <= 0) {/** 滑到第一个 */
                    this.setData({
                        js_index: 0,
                        toleft: this.data.w_width * 0.4
                    })
                    return false;
                } else {
                    this.setData({
                        js_index: ms_index - 1,
                        toleft: this.data.toleft - ms_move
                    })
                }
            } else if (endPoint >= right_click){/**点击右边的 */
                if (ms_index + 1 >= this.data.ms_tablist_len) {/** 滑到最后一个 */
                    return false;
                } else {
                    this.setData({
                        js_index: ms_index + 1,
                        toleft: this.data.toleft + ms_move
                    })
                }
            }
        } else {
            if (distance > 0) {/**向右滑 */
                if (ms_index + 1 >= this.data.ms_tablist_len) {/** 滑到最后一个 */
                    return false;
                } else {
                    this.setData({
                        js_index: ms_index + 1,
                        toleft: this.data.toleft + ms_move
                    })
                }
            } else {/**向左滑 */
                if (ms_index <= 0) {/** 滑到第一个 */
                    this.setData({
                        js_index: 0,
                        toleft: this.data.w_width * 0.4
                    })
                    return false;
                } else {
                    this.setData({
                        js_index: ms_index - 1,
                        toleft: this.data.toleft - ms_move
                    })
                }
            }
        }

        var activity_id = this.data.ms_tablist[this.data.js_index].activity_id;
        this.getActiveInfo(activity_id);
    },
    /**显示详情 */
    showDetail: function (e) {
        var itemId = e.currentTarget.id;
        wx.navigateTo({
            url: '/pages/product/detail/detail?itemId=' + itemId
        })
    },
    getAcname: function (id) {
        (new JxbHttpClient()).get({
            url: '/AppMaster/Activity/getActivityInfo',
            data: {
                activity_id: id
            }
        }).then(jxbResp=>{
            let list = jxbResp.data.activity_list;
            for (let i = 0; i < list.length; i++) {
                if (list[i].activity_id == id) {
                    this.setData({
                        js_index: i,
                        toleft: this.data.toleft + (this.data.width) * i
                    })
                }
            }
        }).catch(err => {
            console.log(err)
            wx.showToast({
                title: err.message,
                icon: 'none',
                duration: 2000
            })
        })
    },
    getActiveInfo: function (id) {
        (new JxbHttpClient()).get({
            url: '/AppMaster/Activity/getActivityInfo',
            data: {
                activity_id: id
            },
        }).then(jxbResp=>{
            clearInterval(this.data.ms_interval)
            /**活动名称列表 */
            let list = jxbResp.data.activity_list;
            for (let i = 0; i < list.length; i++) {
                /**开始时间和结束时间 长度修改 */
                let s_time = list[i].begin_time;
                s_time = s_time.substring(5, s_time.lastIndexOf(":"));
                list[i].begin_time = s_time;

                let e_time = list[i].end_time;
                e_time = e_time.substring(5, e_time.lastIndexOf(":"));
                list[i].end_time = e_time;
                
                if (list[i].activity_id == id){
                    /**当前活动的状态 */
                    let ac_state = list[i].state;
                    this.setData({
                        ac_state: ac_state,                                
                    })

                    /**倒计时 */
                    let ms_second = list[i].activity_end_second;
                    this.data.ms_interval = setInterval(function(){
                        ms_second--;
                        if (ms_second > 0) {
                            this.setData({
                                endDate: this.caculateTime(ms_second),
                            });
                        } else {
                            this.setData({
                                endDates: 0,
                            });
                            clearInterval(this.data.ms_interval);
                        }
                    }, 1000); 
                }
            }

            /**活动商品列表 */
            let a_list = jxbResp.data.list;
            for (let j = 0; j < a_list.length; j++) {
                /**已售百分比 */
                let ms_per = (a_list[j].activity_item_num - a_list[j].activity_item_had_num) / a_list[j].activity_item_num;
                a_list[j].sell_per = parseInt(ms_per * 100) + "%";
            }

            this.setData({
                ms_tablist: list,
                ms_alist: a_list,
                ms_tablist_len: list.length
            })
        }).catch(err=>{
            
            wx.showToast({
                title: err.message,
                icon: 'none',
                duration: 2000
            })
        })
    },
    /**
     * 倒计时
     */
    caculateTime: function (e) {
        var leftsecond = parseInt(e);
        //var day1=parseInt(leftsecond/(24*60*60*6)); 
        var day1 = Math.floor(leftsecond / (60 * 60 * 24));
        var hour = Math.floor((leftsecond) / 3600) + '';
        var hour1 = Math.floor((leftsecond - day1 * 24 * 60 * 60) / 3600);
        var minute = Math.floor((leftsecond - day1 * 24 * 60 * 60 - hour1 * 3600) / 60) + '';
        var second = Math.floor(leftsecond - day1 * 24 * 60 * 60 - hour1 * 3600 - minute * 60) + '';
        var divname = {};
        // divname.day = day1;
        divname.hour = hour.length < 2 ? ('0' + hour) : hour;
        divname.minute = minute.length < 2 ? ('0' + minute) : minute;
        divname.second = second.length < 2 ? ('0' + second) : second;
        // console.log(divname);
        return divname;
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    }
})